<template>
  <a-modal title="商品分类管理" :width="800" :visible="visible" :confirmLoading="confirmLoading" :maskClosable="false" @ok="handleSubmit"
           @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">

      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="所属门店">
          <a-select showSearch optionFilterProp="children" :filterOption="filterOption" :disabled="isDisabled"
                    v-decorator="['storeId', {rules: [{required: true, message: '必输'}]}]">
            <a-select-option v-for="item in customers" :key="item.id">{{item.name}}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="代金券批次号">
          <a-input v-decorator="[ 'productNo', {rules: [{required: true, message: '必输'}] }]" :disabled="isDisabled"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="金额">
          <a-input v-decorator="[ 'amount', {rules: [] }]"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="积分">
          <a-input v-decorator="[ 'amount2', {rules: [] }]"/>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="购买后获取积分百分比">
          <a-input v-decorator="[ 'integralRate', {rules: [] }]"/>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="排序">
          <a-input v-decorator="[ 'sortNo', {rules: [] }]"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="每日限额(只针对购买生效)">
          <a-input v-decorator="[ 'onedayQuota', {rules: [] }]"/>
        </a-form-item>



        <a-form-item label="商家券主图" extra="商家券主图推荐500*500" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-upload
            listType="picture-card"
            class="avatar-uploader"
            :showUploadList="false"
            :action="baseUrl + '/system/common/uploadFile'"
            :beforeUpload="beforeUpload"
            @change="uploadChange"
          >
            <img v-if="fileUrl" :src="fileUrl" alt="avatar" width="300px"/>
            <div v-else>
              <a-icon :type="confirmLoading ? 'loading' : 'plus'" />
              <div class="ant-upload-text">Upload</div>
            </div>
          </a-upload>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import { saveOrUpdate } from '@/api/product/voucherCoupon'
  /**
   * 数据
   */
  const data = function() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      id: null,
      visible: false,
      confirmLoading: false,
      isDisabled: false,

      form: this.$form.createForm(this),
      fileUrl:null,
      baseUrl: process.env.VUE_APP_API_BASE_URL,
    }
  }

  const created = function() {

  }

  export default {
    props: {
      customers: {
        type: Array,
        default: null
      }
    },
    data: data,
    created: created,
    /**
     * 方法
     */
    methods: {
      uploadChange(info) {
        if (info.file.status === 'uploading') {
          this.confirmLoading = true;
          return;
        }
        if (info.file.status === 'done') {
          // Get this url from response in real world.
          this.fileUrl = info.file.response.fileUrl;
          this.filename = info.file.response.filename
          this.confirmLoading = false;
        }
      },
      beforeUpload(file) {
        return true;
      },
      add () {
        this.visible = true
      },
      edit (record) {
        this.visible = true
        this.id = record.id
        this.isDisabled = true
        this.fileUrl = record.fileUrl;
        const { form: { setFieldsValue } } = this
        this.$nextTick(() => {
          const formData = pick(record, ['integralRate','storeId', 'productNo','amount','amount2','sortNo','onedayQuota'])
          setFieldsValue(formData)
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            values.fileUrl = this.fileUrl;
            values.id = this.id
            values.productType = 2;
            saveOrUpdate(values).then((res) => {
              this.confirmLoading = false
              if (res.code === 200) {
                this.handleCancel()
                this.$emit('ok')
              }
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.id = null
        this.visible = false
        this.isDisabled = false
        this.confirmLoading = false
        this.form.resetFields()
        this.imageUrl = null
        this.imageUrl2 = null
        this.filename = null
        this.filename2 = null
        this.fileUrl = null
      },
      filterOption (input, option) {
        return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      },

    },
  }
</script>